<template>
  <div class="app-container">
    <el-form v-model="searchParams" label-width="100px":inline="true">
      <el-form-item label="员工工号">
        <el-input v-model="searchParams.staffNo" placeholder="请输入员工工号" clearable/>
      </el-form-item>
      <el-form-item style="margin-left: 10px">
        <el-button type="primary" icon="el-icon-search" v-loading="loading" size="mini" @click="handleQuery">搜索</el-button>
        <el-button style="margin-left: 10px" icon="el-icon-refresh" size="mini" @click="reset">重置</el-button>
        <el-button icon="el-icon-plus" size="mini" @click="startMatrixTask">启动定时任务</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="assessList" v-loading="loading" border :span-method="objectSpanMethod" highlight-current-row>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column type="index" label="序号" align="center"/>
      <el-table-column label="员工工号" prop="staffNo"/>
      <el-table-column label="员工姓名" prop="employeeName"/>
      <el-table-column label="部门名称" prop="deptName"/>
      <el-table-column label="证书编号" prop="certNumber" show-overflow-tooltip/>
      <el-table-column label="取证日期" prop="obtainEvidenceDate"/>
      <el-table-column label="项目代码" prop="projectCode"/>
      <el-table-column label="评估类型" prop="assessType" :formatter="assessTypeFormat"/>
      <el-table-column label="机考分数" prop="theoryScore" :formatter="theoryScoreFormat"/>
      <el-table-column label="领导评价" prop="leadAssess"/>
      <el-table-column label="领导姓名" prop="leadName"/>
    </el-table>
    <pagination
            v-show="total>0"
            :total="total"
            :page.sync="pageParameter.page"
            :limit.sync="pageParameter.rows"
            @pagination="getList"
    />
  </div>
</template>

<script>
  import {searchByPage, startMatrix} from "@/api/assess/examAssess";
  import trainerHandle from "@/views/assess/continuous-assessment/isAssess"
  import leadAssess from "@/views/assess/continuous-assessment/leadAssess";

  export default {
    components: {
      trainerHandle,
      leadAssess
    },
    created() {
      this.getList()
      this.getDicts("assess_type").then(response => {
        this.assessTypeList = response.responseBody.result
      })
    },
    data() {
      return {
        // 总条数
        total: 0,
        // 遮罩层
        loading: false,
        // 分页参数
        pageParameter: {
          page: 1,
          rows: 10
        },
        // 查询参数
        searchParams: {
          // 评估类型，默认只显示矩阵评估
          assessType: 1
        },
        // 评估信息列表
        assessList: [],
        // 评估类型数据字典
        assessTypeList: [],
        // 合并规则对象
        mergeObj: {},
        // 合并的列
        mergeCol: ['staffNo', 'employeeName']
      }
    },
    methods: {

      getList() {
        this.loading = true
        searchByPage({
          pageParameter: this.pageParameter,
          searchParams: this.searchParams
        }).then(response => {
          this.assessList = response.responseBody.result
          this.total = response.responseBody.pageParameter.totalCount
          // 排序
          this.sortByStaffNo(this.assessList)
          // 初始化合并规则
          this.getSpanArr(this.assessList, this.mergeObj)
        }).finally(() => {
          this.loading = false
        })
      },

      handleQuery() {
        this.getList()
      },

      reset() {
        this.searchParams = {}
        this.getList()
      },

      // 评估类型
      assessTypeFormat(row) {
        return this.selectDictLabel(this.assessTypeList, row.assessType)
      },

      // 机考分数
      theoryScoreFormat(row) {
        return row.theoryScore? row.theoryScore : 'N/A'
      },

      // 根据员工工号排序
      sortByStaffNo(list) {
        if(!(list && list.length !== 0)){
          return
        }
        let i = 1 // 当前需要交换位置下标，从第二个元素开始
        let obj = list[0].staffNo // 当前比较的元素，初值为第一个元素
        let temp // 临时变量
        while(i < list.length) {
          for(let j = i+1; j < list.length; j++){
            // 如果找到相等元素则交换位置
            if(obj === list[j].staffNo){
              temp = list[i]
              list[i] = list[j]
              list[j] = temp
              // 更新交换位置
              i += 1
            }
          }
          // 更新比较元素
          obj = list[i].staffNo
          // 更新交换位置
          i += 1
        }
      },

      // 初始化合并规则
      getSpanArr(list) {
        this.mergeCol.forEach((key) => {
          let count = 0; // 用来记录需要合并行的起始位置
          this.mergeObj[key] = []; // 记录每一列的合并信息
          list.forEach((item, index) => {
            // index == 0表示数据为第一行，直接 push 一个 1
            if(index === 0) {
              this.mergeObj[key].push(1);
            } else {
              // 判断当前行是否与上一行其值相等 如果相等 在 count 记录的位置其值 +1 表示当前行需要合并 并push 一个 0 作为占位
              if(item[key] === list[index - 1][key]) {
                this.mergeObj[key][count] += 1;
                this.mergeObj[key].push(0);
              } else {
                // 如果当前行和上一行其值不相等
                count = index; // 记录当前位置
                this.mergeObj[key].push(1); // 重新push 一个 1
              }
            }
          })
        })
      },

      // 合并规则
      objectSpanMethod({ row, column, rowIndex, columnIndex }) {
        // 判断列的属性
        if(this.mergeCol.indexOf(column.property) !== -1) {
          // 判断其值是不是为0
          if(this.mergeObj[column.property][rowIndex]) {
            return [this.mergeObj[column.property][rowIndex], 1]
          } else {
            // 如果为0则为需要合并的行
            return [0, 0];
          }
        }
      },

      // 启动矩阵评估定时任务
      startMatrixTask() {
        startMatrix().then(response => {
          this.$message.success(response.responseBody)
        })
      }
    }
  }
</script>